/*
 * This file is part of animus, a GUI for the command line app "GHost++"
 *
 * http://code.google.com/p/animus/
 *
 * Copyright (C) 2008-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml.Serialization;
using System.Reflection;
using System.IO;

namespace animus
{
    [Serializable]
    public class ConsoleTriggerCollection : CollectionBase
    {
        public ConsoleTriggerCollection()
        {
            //default constructor
        }

        public ConsoleTrigger this[int index]
        {
            get { return (ConsoleTrigger)List[index]; }
            set { List[index] = value; }
        }

        public int Add(ConsoleTrigger trigger)
        {
            return List.Add(trigger);
        }

        public void AddRange(ConsoleTrigger[] triggers)
        {
            foreach (ConsoleTrigger t in triggers)
            {
                List.Add(t);
            }
        }

        public void Remove(ConsoleTrigger trigger)
        {
            List.Remove(trigger);
        }

        public void Insert(int index, ConsoleTrigger trigger)
        {
            List.Insert(index, trigger);
        }

        public void Update(ConsoleTrigger oldTrigger, ConsoleTrigger newTrigger)
        {
            int i = List.IndexOf(oldTrigger);
            List.Remove(oldTrigger);
            List.Insert(i, newTrigger);
        }

        public int IndexOf(ConsoleTrigger trigger)
        {
            return List.IndexOf(trigger);
        }

        /*public ConsoleTrigger[] Triggers
        {
            get
            {
                ConsoleTrigger[] triggers = new ConsoleTrigger[List.Count];
                List.CopyTo(triggers, 0);
                return triggers;
            }
            set
            {
                List.Clear();
                AddRange(value);
            }
        }*/

        public void Save(string file)
        {
            Type[] alltypes = Assembly.GetAssembly(typeof(ConsoleTrigger)).GetTypes();
            List<Type> derived = new List<Type>();
            derived.Add(typeof(ConsoleTrigger));
            foreach (Type t in alltypes)
            {
                if (t.IsSubclassOf(typeof(ConsoleTrigger)))
                    derived.Add(t);
            }
            Type[] compatible = new Type[derived.Count];
            derived.CopyTo(compatible, 0);
            XmlSerializer s = new XmlSerializer(this.GetType(), compatible);
            using (StreamWriter w = new StreamWriter(file))
            {
                s.Serialize(w, this);
                w.Flush();
            }
        }
        public void Load(string file)
        {
            Type[] alltypes = Assembly.GetAssembly(typeof(ConsoleTrigger)).GetTypes();
            List<Type> derived = new List<Type>();
            derived.Add(typeof(ConsoleTrigger));
            foreach (Type t in alltypes)
            {
                if (t.IsSubclassOf(typeof(ConsoleTrigger)))
                    derived.Add(t);
            }
            Type[] compatible = new Type[derived.Count];
            derived.CopyTo(compatible, 0);
            XmlSerializer s = new XmlSerializer(this.GetType(), compatible);
            using (StreamReader r = new StreamReader(file))
            {
                ConsoleTriggerCollection newCollection = (ConsoleTriggerCollection)s.Deserialize(r);
                foreach (ConsoleTrigger t in newCollection)
                {
                    Add(t);
                }
            }
        }
    }
}
